package com.pet.util;

import java.io.File;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

public class UploadUtil {
	public static String getImagePath(HttpServletRequest request) {
		String tomcatRoot = request.getSession().getServletContext()
				.getRealPath("/");
		tomcatRoot.replace("/", "\\\\");
		String[] foo = tomcatRoot.split("\\\\");
		StringBuilder tomcatWebAppsBuilder = new StringBuilder();
		int i = 0;
		for (String paths : foo) {
			++i;
			if (i != foo.length) {
				tomcatWebAppsBuilder.append(paths);
				tomcatWebAppsBuilder.append("/");
			}
		}
		String imagePath = tomcatWebAppsBuilder.toString() + "/img/";
		File f = new File(imagePath);
		if (!f.exists()) {
			f.mkdirs();
		}
		return imagePath;
	}
}
